package prototype.copy;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student implements Cloneable{

    private String name;
    private Teacher teacher;

    /**
     * 浅拷贝
     * @return 克隆结果与原对象的引用类型指向同一内存地址
     */
//    @Override
//    public Student clone() {
//        try {
//            return (Student)super.clone();
//        } catch (CloneNotSupportedException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }

    /**
     * 深拷贝
     * @return 克隆结果中引用类型也是新的克隆
     */
    @Override
    public Student clone() {
        Student student = null;
        try {
            student = (Student)super.clone();
            student.teacher = this.teacher.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return student;
    }
}
